const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 299,
        fixedAtt: 19,
        fixdefence: 23,
        reaction : 23,
        attRate: 5.8, 
        hpRate: 5.8, 
        criticalRate: 3.9,
        criticalStrike: 7.8,
        defRate: 7.3,
        charging: 6.5
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}


const CLIPDATA = CLIPDATA_TYPE.max


const CONFIG = {
    chance: 25,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        defRate: 51.8,
        dmgRate: 46.6
    },
    clips: ["attRate", "defRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["attRate", "defRate", "criticalRate", "criticalStrike"],
        ["attRate", "defRate", "criticalRate", "criticalStrike"],
        ["attRate", "charging", "criticalRate", "criticalStrike"],
        ["attRate", "defRate", "criticalRate", "criticalStrike"],
        ["defRate", "charging", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { attRate: 18 },
        // { dmgRate: 24, defRate:54 },
        { dmgRate: 15},
        { dmgZhi:2000,dmgRate:15},
        { fixedAtt: 1202, attRate: 20},
        { defRate:25, criticalStrike: 40, dmgRate: 15},
    ]
}
        
let charaters = {
    units : {
        name: "女仆",    
        charging: 100,  
        defence: 799,
        defRate:30,
        dmgZhi:0,
        hp:12071,
        base: 191,        
        criticalRate: 5,
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    }
}

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
            count++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
            let defence = data.defence * (1 + data.defRate/100) + 438;
            data.defence = defence
            if(weapon.skill) { weapon.skill(data); }
            let def2At = 1.35 * defence
            data.fixedAtt += def2At;
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            data.dmgZhi += attack*0.35
            // 2.2423
            let aP1 = getDmg({ data, rate: 1.56}).qw * reduceResistanceK(-10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let aP2 = getDmg({ data, rate: 1.45}).qw * reduceResistanceK(-10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let aP3 = getDmg({ data, rate: 1.71}).qw * reduceResistanceK(-10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let aP4 = getDmg({ data, rate: 2.24}).qw * reduceResistanceK(-10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            
            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }

            let point = (aP1 + aP2 + aP3 + aP4) * 5 + (data.phyDmg||0)
            return {point, data, second, attack, dmg:{aP1,aP2,aP3,aP4},def2At}
        }))
    });
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point)
    });
    console.log(charactersDmg[7]);
    console.log(charactersDmg[8]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
